package com.zxx.pattern.adapterPattern;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zxx.designpattern.R;

import java.util.List;

/**
 * @author zxx on 2021/7/12
 */
public class CircleMenuAdapter extends BaseAdapter {

    private List<CircleMenuLayout.MenuItem> mMenuItems;

    public CircleMenuAdapter(List<CircleMenuLayout.MenuItem> mMenuItems) {
        this.mMenuItems = mMenuItems;
    }

    @Override
    public int getCount() {
        return mMenuItems.size();
    }

    @Override
    public Object getItem(int position) {
        return mMenuItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View itemView = inflater.inflate(R.layout.circle_menu_item, parent, false);
        initMenuItem(itemView, position);
        return itemView;
    }

    private void initMenuItem(View itemView, int position) {
        CircleMenuLayout.MenuItem item = (CircleMenuLayout.MenuItem) getItem(position);
        ImageView iv = itemView.findViewById(R.id.circle_menu_image);
        TextView tv = itemView.findViewById(R.id.circle_menu_text);
        iv.setImageResource(item.imageId);
        tv.setText(item.title);
    }
}
